package cn.edu.sctu.sctu_lysd.base;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;


import com.umeng.socialize.Config;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.common.QueuedWork;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.edu.sctu.sctu_lysd.utils.L;
import io.rong.imkit.RongIM;


/**
 * Created by Administrator on 2016/4/28.
 */
public class BaseApplication extends Application {

    private static BaseApplication mInstance;
    private List<Activity> activityList = new LinkedList<Activity>();

    private ExecutorService threadPool;
    public ExecutorService getThreadPool(){
        if (threadPool == null){
            threadPool = Executors.newCachedThreadPool();
        }
        return threadPool;
    }

    public static BaseApplication getInstance() {
        return mInstance;
    }

    // 添加Activity到容器中
    public void addActivity(Activity activity) {
        activityList.add(activity);
    }

    // 遍历所有Activity并finish
    public void exit() {

        if (RongIM.getInstance() != null && RongIM.getInstance().getRongIMClient() != null) {
            /**
             * 设置连接状态变化的监听器.
             */
            RongIM.getInstance().disconnect();
        }

        for (Activity activity : activityList) {
            activity.finish();
        }
        L.d("activityList==Size:", "" + activityList);

    }

    public static Map<String, Long> map;//用来存放倒计时的时间

    @Override
    public void onCreate() {
        super.onCreate();
        RongIM.init(this);
        mInstance = this;

//        Config.DEBUG = true;
//        QueuedWork.isUseThreadPool = false;
//        UMShareAPI.get(this);

        PlatformConfig.setWeixin("wxc415a73a5c407adb", "f61322db044b0acb506570dcd2f9aee0");
        PlatformConfig.setQQZone("1105564855", "AkiHUPvsk4qHcIkF");

    }

    /**
     * 获得当前进程的名字
     *
     * @param context
     * @return
     */
    public static String getCurProcessName(Context context) {

        int pid = android.os.Process.myPid();

        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);

        for (ActivityManager.RunningAppProcessInfo appProcess : activityManager
                .getRunningAppProcesses()) {

            if (appProcess.pid == pid) {

                return appProcess.processName;
            }
        }
        return null;
    }


}
